import numpy as np

# X_train is made of x[i].T .
# x[i] is a column vector and theta,y

# load the data
X_train = np.loadtxt('X_train.txt', delimiter=',')
y_train = np.loadtxt('y_train.txt', delimiter=',')
X_test = np.loadtxt('X_test.txt', delimiter=',')
y_test = np.loadtxt('y_test.txt', delimiter=',')
n = X_train.shape[1]#feature number
#
theta = np.zeros((n+1,1))
assert theta.shape == (n+1,1)
iteration = 100
learning_rate = 0.01

#grad descent
for i in range(iteration):
    theta = theta - learning_rate * np.dot(X_train.T,(np.dot(X_train,theta) - y_train))

#norm equoation
theta = np.dot(np.dot(np.linalg.inv(np.dot(X_train.T,X_train)), X_train.T),y_train)

#predict
prediction = np.dot(X_test.T,theta)
